﻿using System;
using System.Collections.Generic;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.Filter;

namespace Slippor.Douban.Radio.BaseForm
{
    public class FilterCollection : Dictionary<String, IFilter>
    {
        public object ForLock = new object();

        public bool Filter(ref Song song)
        {
            lock (ForLock)
            {
                foreach (IFilter filter in Values)
                {
                    if (filter.Filter(ref song))
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        public void Update()
        {
            lock (ForLock)
            {
                String value = String.Empty;
                foreach (String key in Keys)
                {
                    if (!String.IsNullOrEmpty(key))
                    {
                        if (key.IndexOf('#') == 0)
                        {
                            if (key != EnglishFilter.Key && key != ChineseFilter.Key)
                            {
                                continue;
                            }
                        }
                        value = value + "," + key;
                    }
                }
                value = value.TrimStart(',');
                StaticResources.SettingManager.SetString("Filters", value);
            }
        }

        public void NormalClear()
        {
            lock (ForLock)
            {
                List<String> filterKeys = new List<string>();
                foreach (String key in Keys)
                {
                    if (key.IndexOf('#') == 0)
                    {
                        if (key == EnglishFilter.Key || key == ChineseFilter.Key)
                        {
                            filterKeys.Add(key);
                        }
                    }
                    else
                    {
                        filterKeys.Add(key);
                    }
                }
                foreach (var filterKey in filterKeys)
                {
                    Remove(filterKey);
                }
            }
        }
    }

    public class NormalFilter : IFilter
    {
        public const String Key = "#Nornaml";

        #region IFilter Members

        public bool Filter(ref Song song)
        {
            if ((song.Title.IndexOf(Song.DefaultTitle) == 0 && song.Title.IndexOf("Beta") == 7) || (song.Title.IndexOf('-') == -1) || song.Title.IndexOf(Song.DefaultTitle) == -1)
            {
                return true;
            }
            song.Title = song.Title.Substring(6).Trim();
            return false;
        }

        public string Name
        {
            get { return Key; }
        }

        #endregion
    }

    public class EnglishFilter : IFilter
    {
        public const String Key = "#EN";

        #region IFilter Members

        public bool Filter(ref Song song)
        {
            bool _filter = false;
            foreach (char x in song.Title)
            {
                if (x > 0x4e00 && x <= 0x9fa5)
                {
                    return false;
                }
                if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'))
                {
                    _filter = true;
                }
            }
            return _filter;
        }

        public string Name
        {
            get { return Key; }
        }

        #endregion
    }

    public class ChineseFilter : IFilter
    {
        public const String Key = "#CN";

        #region IFilter Members

        public bool Filter(ref Song song)
        {
            foreach (char x in song.Title)
            {
                if (x > 0x4e00 && x <= 0x9fa5)
                {
                    return true;
                }
            }
            return false;
        }

        public string Name
        {
            get { return Key; }
        }

        #endregion
    }

    public class CustomFilter : IFilter
    {
        private readonly String _key;

        public CustomFilter(String key)
        {
            _key = key;
        }

        #region IFilter Members

        public bool Filter(ref Song song)
        {
            if (song.Title.IndexOf(_key) != -1)
            {
                return true;
            }
            return false;
        }

        public string Name
        {
            get { return _key; }
        }

        #endregion
    }
}